package com.achievo.reportserver.config;

import java.io.File;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.commons.jci.monitor.FilesystemAlterationListener;
import org.apache.commons.jci.monitor.FilesystemAlterationObserver;
import org.apache.log4j.Logger;

import com.achievo.reportserver.mq.JmsConfig;


/**
 * 
 * Report Server
 * write at Jan 21, 2010
 * @author <a href="mailto:chinaoryx@gmail.com">Hyman.Zhang (Zhang Haipeng)</a>
 * @version 1.0
 * 
 *
 */
public class ReportConfigFileListener implements FilesystemAlterationListener {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ReportConfigFileListener.class);

	public void onDirectoryChange(File pDir) {
		if (logger.isInfoEnabled()) {
			logger.info("onDirectoryChange(File=" + pDir + ")");
		}
	}

	public void onDirectoryCreate(File pDir) {
		if (logger.isInfoEnabled()) {
			logger.info("onDirectoryCreate(File=" + pDir + ")");
		}

	}

	public void onDirectoryDelete(File pDir) {
		if (logger.isInfoEnabled()) {
			logger.info("onDirectoryDelete(File=" + pDir + ")");
		}

	}

	public void onFileChange(File pFile) {
		if (logger.isDebugEnabled()) {
			logger.debug("onFileChange(File=" + pFile + ") - start");
		}
		String name = pFile.getName();
		
		if (name.endsWith(".xml")) {
			this.sendMsg("fileChange", pFile.getAbsolutePath());
		} else {
			this.sendMsg("jasperUpdate", pFile.getAbsolutePath());
		}
		

		if (logger.isDebugEnabled()) {
			logger.debug("onFileChange(File=" + pFile + ") - end");
		}
	}

	public void onFileCreate(File pFile) {
		if (logger.isDebugEnabled()) {
			logger.debug("onFileCreate(File=" + pFile + ") - start");
		}
		
		String name = pFile.getName();
		if (name.endsWith(".xml")) {
			this.sendMsg("fileCreate", pFile.getAbsolutePath());
		} else {
			this.sendMsg("jasperUpdate", pFile.getAbsolutePath());
		}
	
    	

		if (logger.isDebugEnabled()) {
			logger.debug("onFileCreate(File=" + pFile + ") - end");
		}
	}

	public void onFileDelete(File pFile) {
		if (logger.isDebugEnabled()) {
			logger.debug("onFileDelete(File=" + pFile + ") - start");
		}
		String name = pFile.getName();
		if (name.endsWith(".xml")) {
			this.sendMsg("fileDelete", pFile.getAbsolutePath());
		} else {
			this.sendMsg("jasperUpdate", pFile.getAbsolutePath());
		}

		if (logger.isDebugEnabled()) {
			logger.debug("onFileDelete(File=" + pFile + ") - end");
		}
	}

	public void onStart(FilesystemAlterationObserver pObserver) {
//		if (logger.isInfoEnabled()) {
//			logger.info("onStart(FilesystemAlterationObserver=" + pObserver + ")");
//		}

	}

	public void onStop(FilesystemAlterationObserver pObserver) {
//		if (logger.isInfoEnabled()) {
//			logger.info("onStop(FilesystemAlterationObserver=" + pObserver + ")");
//		}

	}
	
	
	private void sendMsg(String eventType, String filepath){
		Connection conn = JmsConfig.getConfigProducerConn();
		// create javax.jms.Session
    	try {
			Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Queue queue = session.createQueue(JmsConfig.QUEUE_CONFIG);
			MessageProducer producer = session.createProducer(queue);      
	    	producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);    	
	    	TextMessage msg = session.createTextMessage(eventType);
	    	msg.setStringProperty("filepath", filepath);	    	
	    	producer.send(msg);  
		} catch (JMSException e) {
			logger.error("onFileCreate", e);
		}
	}

}
